package com.fenqing.leetcode.code.t300_400;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.*;

public class Topic347 extends Topic {

    /**
     * 构造器
     *
     * @param input1 输入
     * @param input2 输入
     */
    public Topic347(String input1, String input2) {
        super(input1, input2);
    }

    @Execute
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        for (int num : nums) {
            Integer val = hash.getOrDefault(num, 0);
            hash.put(num, val + 1);
        }
        Set<Map.Entry<Integer, Integer>> entries = hash.entrySet();
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(entries);
        list.sort((o1, o2) -> o2.getValue() - o1.getValue());
        int[] arr = new int[k];
        for (int i = 0; i < k; i++) {
            arr[i] = list.get(i).getKey();
        }
        return arr;
    }

    public static void main(String[] args) {
        Topic347 topic347 = new Topic347(
                """
                [1,1,1,2,2,3]
                """,
                "2"
        );
        int[] run = topic347.run();
        System.out.println(Arrays.toString(run));
    }

}
